<template>
  <div>
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="查看状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择状态"
            clearable
            class="!w-240px"
          >
            <el-option
              v-for="dict in getIntDictOptions(DICT_TYPE.PUBLIC_VIEW_STATUS)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="主题" prop="theme">
          <el-input
            v-model="queryParams.theme"
            placeholder="请输入主题"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="queryParams.createTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column
          label="序号"
          type="index"
          header-align="center"
          align="center"
          width="60px"
        />
        <el-table-column label="主题" align="center" prop="theme" />
        <el-table-column label="有无附件" align="center" prop="fileExists">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.PUBLIC_YON_STATUS" :value="scope.row.fileExists" />
          </template>
        </el-table-column>
        <el-table-column label="内容简介" align="center" prop="content" show-overflow-tooltip />
        <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
        <el-table-column label="首次查看时间" align="center" prop="firstViewTime" :formatter="dateFormatter" width="180px" />
        <el-table-column label="末次查看时间" align="center" prop="lastViewTime" :formatter="dateFormatter" width="180px" />
        <el-table-column label="查看状态" align="center" prop="state">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.PUBLIC_VIEW_STATUS" :value="scope.row.state" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="details(scope.row.id, scope.row.officialLetterId)"
            >
              查阅
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </div>

  <!-- 表单弹窗：详情 -->
  <el-drawer
    v-model="drawer"
    title="详情"
    :direction="direction"
    v-if="drawer"
    size="71%"
    class="drawer"
    destory-on-close
    @close="getList"
  >
    <DetailForm ref="detailRef" :detailId="detailId" />
  </el-drawer>
</template>

<script setup lang="ts">
import { BaseOfficialLetterApi, BaseOfficialLetterVO, BaseOfficialLetterDetailVO } from '@/api/infra/baseofficialletter'
import DetailForm from './view/DetailForm.vue'

import { getIntDictOptions } from '@/utils/dict'
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'

/** 公函 列表 */
defineOptions({ name: 'BaseOfficialLetter' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<BaseOfficialLetterVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  theme: undefined,
  remark: undefined,
  createTime: [],
  publishTime: [],
  status: undefined,
  flag: 'invalid'
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await BaseOfficialLetterApi.getEffectOfficialLetterPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
const formData = ref({
  id: undefined, // 主键
  officialLetterId: undefined, // 公函管理id
  totalViewNum: undefined, // 查看总次数
  status: undefined // 状态(草稿:0；审核中:1；审核通过:2；审核驳回:3)
})

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined, // 主键
    officialLetterId: undefined, // 公函管理id
    totalViewNum: undefined, // 查看总次数
    status: undefined // 状态(草稿:0；审核中:1；审核通过:2；审核驳回:3)
  }
}

/** 查看详情 */
const detailRef = ref()
const drawer = ref(false)
const detailId = ref()
const details = async (id: number, officialLetterId: number) => {

  const data = formData.value as unknown as BaseOfficialLetterDetailVO
  data.id = id
  data.totalViewNum = 1
  data.status = 1
  await BaseOfficialLetterApi.updateLetterDetail(data)

  drawer.value = true
  detailId.value = officialLetterId
}

/** 初始化 **/
onMounted(() => {
  getList()
  resetForm()
})
</script>

<style scoped>
.el-card {
  border: 0px solid var(--el-card-border-color) !important;
}
</style>